<template>
  <div>
    <!-- 路径导航区域 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>师资管理</el-breadcrumb-item>
      <el-breadcrumb-item>擅长领域</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 搜索区域 -->
    <el-row>
      <el-form :inline="true">
        <el-form-item label="领域名称">
          <el-input
            v-model="queryInfo.FieldName"
            clearable
            placeholder="请输入领域名称"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="queryList">搜索</el-button>
          <el-button type="primary" @click="toAddField">添加</el-button>
        </el-form-item>
      </el-form>
    </el-row>

    <!-- 表格区域 -->
    <el-table :data="FieldList" border style="width: 100%">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="fieldId" label="ID"></el-table-column>
      <el-table-column prop="fieldName" label="领域名称"> </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-link type="primary" @click="toEditField(scope.row)">编辑</el-link>
          <el-link type="primary" @click="deleteField(scope.row.fieldId)"
            >删除</el-link
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页区域 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pageNum"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="queryInfo.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>

    <!-- 新增和修改的对话框 -->
    <el-dialog :title="title" :visible.sync="dialogFormVisible" @close="cancle">
      <el-form :model="field">
        <el-form-item label="领域名称" label-width="80px">
          <el-input
            placeholder="请输入领域名称"
            v-model="field.fieldName"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancle">取 消</el-button>
        <el-button type="primary" @click="addField">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 查询信息对象
      queryInfo: {
        fieldName: "",
        pageNum: 1,
        pageSize: 5,
      },
      //等级对象
      field: {
        fieldId: "",
        fieldName: "",
      },

      total: 183,
      FieldList: [],
      dialogFormVisible: false,
      title: "",
    };
  },

  methods: {
    //点击增加打开对话框
    toAddField() {
      this.dialogFormVisible = true;
      this.title = "添加领域";
    },
    //点击修改打开对话框
    toEditField(row) {
      this.field = row;
      this.title = "修改领域";
      this.dialogFormVisible = true;
    },

    //添加或修改的方法
    addField() {
      if (this.field.fieldId) {
        //有id值为修改
        this.$http.put("field/editField", this.field).then((res) => {
          if (res.data.status == 200) {
            this.$message.success("修改成功！");
            //清楚数据
            this.field = {
              fieldId: "",
              fieldName: "",
            };
            this.dialogFormVisible = false;
            this.queryList();
          } else {
            this.$$message.error("修改失败！");
          }
        });
      } else {
        //添加
        this.$http.post("field/addField", this.field).then((res) => {
          if (res.data.status == 200) {
            this.$message.success("添加成功！");
            this.field = {
              fieldId: "",
              fieldName: "",
            };
            //关掉对话框
            this.dialogFormVisible = false;
            //重新查询
            this.queryList();
          } else {
            this.$message.error("添加失败！");
          }
        });
      }
    },

    //删除领域
    deleteField(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$http.delete("field/removeField/" + id).then((resp) => {
            if (resp.data.status == 200) {
              this.$message.success(resp.data.message);
              this.queryList();
            } else {
              this.$message.error(resp.data.message);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    //分页查询
    queryList() {
      //调用后端接口获取数据
      this.$http
        .get("field/findByPage", {
          params: this.queryInfo,
        })
        .then(res => {
          if (res.data.status == 200) {
            console.log(res.data);
            this.FieldList = res.data.data.list;
            this.total = res.data.data.total;
          } else {
            this.$message.error("查询失败！");
          }
        });
    },

    //取消事件
    cancle() {
      this.dialogFormVisible = false;
      this.field = {
        fieldName: "",
      };
    },

    handleSizeChange(size) {
      //   console.log("每页显示数量发生改变");
      this.queryInfo.pageSize = size;
      this.queryInfo.pageNum = 1;
      //重新查询
      this.queryList();
    },
    handleCurrentChange(num) {
      //   console.log("页码发生改变");
      this.queryInfo.pageNum = num;
      //重新查询
      this.queryList();
    },
  },

  // 页面加载时就查询数据
  mounted() {
    this.queryList();
  },
};
</script>

<style>
.el-breadcrumb {
  margin-bottom: 15px;
  padding: 0 0 15px;
  border-bottom: 1px solid #ddd;
}

.el-pagination {
  margin-top: 15px;
}

.el-link {
  padding: 0 5px;
}
</style>